﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;

namespace Study.App_Code
{
    public class PageLang : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            
            base.InitializeCulture();

            HttpCookie cookie = new HttpCookie("USER_COOKIE");

            string lang = "";
            if (Request.QueryString["lang"] != null)
            {
                lang = Request.QueryString["lang"];
                cookie.Values.Add("lang", Request.QueryString["lang"]);
                
                Response.Cookies.Add(cookie);

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            }
            else if (Request.Cookies["USER_COOKIE"] != null)
            {
                cookie = Request.Cookies["USER_COOKIE"];
                if (cookie["lang"] != null)
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie["lang"]);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie["lang"]);
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
                }
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
            }

        }
    }
}